{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {},
   "outputs": [],
   "source": [
    "import cv2 as cv\n",
    "import matplotlib.pyplot as plt\n",
    "import numpy as np\n",
    "from vision import recognition"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {},
   "outputs": [
    {
     "name": "stderr",
     "output_type": "stream",
     "text": [
      "YOLOv5 🚀 v7.0-116-g5c91dae Python-3.8.10 torch-2.0.0+cu117 CUDA:0 (NVIDIA GeForce RTX 3060 Laptop GPU, 5938MiB)\n",
      "\n",
      "Fusing layers... \n",
      "YOLOv5s_my summary: 157 layers, 7042489 parameters, 0 gradients, 15.9 GFLOPs\n",
      "Adding AutoShape... \n"
     ]
    }
   ],
   "source": [
    "vr = recognition.Visual_recognition(\"/home/lyh/uarm_ws/uarm_visual/visual_recognition/yolov5\",\"/home/lyh/uarm_ws/uarm_visual/visual_recognition/weights/best.pt\",conf=0.6)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {},
   "outputs": [],
   "source": [
    "cap = cv.VideoCapture(4)\n",
    "while True:\n",
    "    _,img = cap.read()\n",
    "    cv.imshow(\"image\",img)\n",
    "    if cv.waitKey(10) == 27:\n",
    "        break\n",
    "cap.release()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "{'class_id': 8, 'class_name': 'oral_liquid_bottle', 'xmin': 87.8943862915039, 'ymin': 278.3211669921875, 'xmax': 196.61154174804688, 'ymax': 371.9022216796875, 'conf': 0.9253931045532227}\n",
      "{'class_id': 0, 'class_name': 'toothbrush', 'xmin': 114.69715881347656, 'ymin': 149.2752685546875, 'xmax': 206.32395935058594, 'ymax': 233.12081909179688, 'conf': 0.7732877135276794}\n"
     ]
    }
   ],
   "source": [
    "img2,object = vr.image_detect(img)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {},
   "outputs": [],
   "source": [
    "cv.imshow(\"image1\",img2)\n",
    "cv.waitKey()\n",
    "cv.destroyAllWindows()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {},
   "outputs": [],
   "source": [
    "from arm_control import uarm_control\n",
    "import numpy as np\n",
    "\n",
    "mtx = np.load('../data/camera_param.npz')['mtx']\n",
    "with np.load('../data/eyehand_Matrix.npz') as X:\n",
    "    R, t = [X[i] for i in ('R', 't')]\n",
    "\n",
    "arm = arm = uarm_control.Arm_controller(mtx,R,t)\n",
    "arm.arm_init()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "110.85445803957835 77.53530793399251\n",
      "121.42956561483851 76.45135826803978\n"
     ]
    }
   ],
   "source": [
    "wh = arm.make_warehouse([180,80,10],[2,2,2],[30,30,10])\n",
    "arm.move([200,0,150])\n",
    "i=0\n",
    "for obj in object:\n",
    "    i+=1\n",
    "    x = (obj[\"xmax\"]-obj[\"xmin\"])/2+obj[\"xmin\"]\n",
    "    y = (obj[\"ymax\"]-obj[\"ymin\"])/2+obj[\"ymin\"]\n",
    "    pos = arm.img_point(x,y,1)\n",
    "    arm.gate_move([pos[0],pos[1],pos[2]], wh[i], 50)\n",
    "    arm.move([200, 0, 150])"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.8.10"
  },
  "orig_nbformat": 4
 },
 "nbformat": 4,
 "nbformat_minor": 2
}
